package model

import ClickHouse "gin/database"

type Module struct {
	Id     int64  `json:"id" gorm:"column:id"`
	Type   string `json:"type" gorm:"column:type"`
	Active string `json:"active" gorm:"column:active"`
	Name   string `json:"name" gorm:"column:name"`
	Code   string `json:"code" gorm:"column:code"`
	Icon   string `json:"icon" gorm:"column:icon"`
	Uuid   string `json:"uuid" gorm:"column:uuid"`
}

// TableName 设置表名
func (Module) TableName() string {
	return "module"
}

// GetByCode 通过code获取module
func (m *Module) GetByCode(code string) (res Module, err error) {
	err = ClickHouse.DB.Model(m).Where("code = ?", code).First(&res).Error
	return
}

// GetByCode 通过code获取module
func (m *Module) GetById(code uint64) (res Module, err error) {
	err = ClickHouse.DB.Model(m).Where("id = ?", code).First(&res).Error
	return
}

// GetByCode 通过code获取module
func (m *Module) GetByCodesMap(codes []string) (res map[string]Module, err error) {
	var modules []Module
	err = ClickHouse.DB.Model(m).Where("code in ?", codes).Find(&modules).Error

	if err != nil {
		return
	}

	res = map[string]Module{}
	for _, moduleItem := range modules {
		res[moduleItem.Code] = moduleItem
	}

	return
}

// GetByCode 通过code获取module
func (m *Module) GetByIdsMap(ids []int64) (res map[int64]Module, err error) {
	var modules []Module
	err = ClickHouse.DB.Model(m).Where("id in ?", ids).First(&modules).Error

	res = map[int64]Module{}
	for _, moduleItem := range modules {
		res[moduleItem.Id] = moduleItem
	}

	return
}
